﻿using NetEasyMusic.Lib.Models;

namespace NetEasyMusic.Lib.Data;

/// <summary>
///     现在只做了二维码登录跟游客登陆
/// </summary>
public class Login : DataBasic
{
    public Login(string cookie) : base(cookie)
    {
    }

    public Login()
    {
    }

    public Login(string url, bool isUse = false) : base(url, isUse)
    {
    }

    private string? Token { get; set; }

    /// <summary>
    /// 游客登录
    /// </summary>
    /// <returns>Cookie</returns>
    public async Task<string> Visitor()
    {
        var context = await GetFormString("/register/anonimous");
        return context == null ? "" : context["cookie"]!.ToString();
    }

    #region 二维码登录

    /// <summary>
    /// 获取登录二维码数组
    /// </summary>
    /// <returns></returns>
    public async Task<byte[]> QrLogin()
    {
        await GetToken();
        var context = await GetFormString(
            $"/login/qr/create?key={Token}&qrimg=1&timestamp={DateTime.UtcNow.ToFileTime()}");
        if (context == null) return [];
        var base64 = context["data"]?["qrimg"]?.ToObject<string>();
        if (string.IsNullOrEmpty(base64)) return [];
        base64 = base64.Replace("data:image/png;base64,", "").Replace("data:image/jgp;base64,", "")
            .Replace("data:image/jpg;base64,", "").Replace("data:image/jpeg;base64,", "");
        return Convert.FromBase64String(base64);
    }


    /// <summary>
    /// 获取中间Token
    /// </summary>
    private async Task GetToken()
    {
        var context = await GetFormString($"/login/qr/key?timestamp={DateTime.UtcNow.ToFileTime()}");
        Token = context?["data"]?["unikey"]?.ToObject<string>();
    }


    /// <summary>
    /// 检查
    /// </summary>
    /// <returns>Cookie</returns>
    public async Task<string> Check()
    {
        var context = await GetFormString($"/login/qr/check?key={Token}");
        return context == null ? "" : context["cookie"]!.ToString();
    }

    #endregion

    #region 登录

    /// <summary>
    /// 手机登录
    /// </summary>
    /// <param name="phone">手机号码</param>
    /// <param name="password">密码</param>
    /// <returns>Cookie 和 用户信息</returns>
    public async Task<(string, UserModel)> PhoneLogin(string phone, string password)
    {
        var context = await GetFormString($"/login/cellphone?phone={phone}&password={password}");
        if (context == null) return ("", new UserModel());
        var id = context["account"]!["id"]!.ToObject<string>()!;
        var user = new UserModel
        {
            UserId = id,
            Nickname = context["profile"]!["nickname"]!.ToObject<string>()!,
            AvatarUrl = context["profile"]!["avatarUrl"]!.ToObject<string>()!,
            IsVisitor = false
        };

        return (context["cookie"]!.ToString(), user);
    }


    /// <summary>
    /// 邮箱登录
    /// </summary>
    /// <param name="mail">邮箱</param>
    /// <param name="password">密码</param>
    /// <returns>Cookie 和 用户信息</returns>
    public async Task<string> MailLogin(string mail, string password)
    {
        var context = await GetFormString($"/login?email={mail}&password={password}");
        return context == null ? "" : context["cookie"]!.ToString();
    }

    #endregion
}